package ru.mipt.sport.model.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import ru.mipt.sport.model.interfaces.RunResult;

/**
 *
 * @author Danon
 */
@Entity
public class RunResultEntity implements Serializable, RunResult, Comparable<RunResult> {

    private static final long serialVersionUID = 1L;
    
    public static final String TYPE_TIME_MILLIS = "TIME_MILLIS";
    public static final String TYPE_INTEGER = "INTEGER";
    public static final String TYPE_FLOAT = "FLOAT";
    public static final String TYPE_STRING = "STRING";
    
    public static final String STRATEGY_NORMAL = "NORAML";
    public static final String STRATEGY_NON_COPETING = "NON_COMPETING";
    
    
    @Id @Column(name="run_result_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @JoinColumn(name="competition_run_id", referencedColumnName="competition_run_id")
    @ManyToOne
    private CompetitionRunEntity competitionRun;
    
    @Column(name="name")
    private String name;
    
    @Column(name="strategy")
    private String starategy = STRATEGY_NORMAL;
    
    // INTEGER, FLOAT, STRING, TIME_MILLIS
    @Column(name = "result_type")
    private String type = TYPE_FLOAT;
    
    @Column(name = "units")
    private String units;
    
    @Column(name = "result_value")
    private String totalValue;
    
    @Column(name = "team")
    private String team;
    
    // MALE, FEMALE, NULL
    @Column(name = "gender")
    private String gender;
    
    @OneToMany(fetch= FetchType.EAGER, cascade= CascadeType.ALL)
    private List<RunResultEntryEntity> resultEntries;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUnits() {
        return units;
    }

    public void setUnits(String units) {
        this.units = units;
    }

    public CompetitionRunEntity getCompetitionRun() {
        return competitionRun;
    }

    public void setCompetitionRun(CompetitionRunEntity competitionRun) {
        this.competitionRun = competitionRun;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTotalValue() {
        return totalValue;
    }

    public void setTotalValue(String totalValue) {
        this.totalValue = totalValue;
    }

    public List<RunResultEntryEntity> getResultEntries() {
        return resultEntries;
    }

    public void setResultEntries(List<RunResultEntryEntity> resultEntries) {
        this.resultEntries = resultEntries;
    }

    public String getStarategy() {
        return starategy;
    }

    public void setStarategy(String starategy) {
        this.starategy = starategy;
    }

    public String getTeam() {
        return team;
    }

    public void setTeam(String team) {
        this.team = team;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof RunResultEntity)) {
            return false;
        }
        RunResultEntity other = (RunResultEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ru.mipt.sport.model.entity.RunResultEntity[ id=" + id + " ]";
    }

    @Override
    public int compareTo(RunResult o) {
        if (o == null)
            return 1;
        try {
            if (!this.getType().equalsIgnoreCase(o.getType()))
                return 0;
            if (!this.getUnits().equalsIgnoreCase(o.getUnits()))
                return 0;
            return new Long(this.getTotalValue()).compareTo(new Long(o.getTotalValue()));
        } catch (Exception ex) {
            System.err.println("Comparison error!\n" + ex);
        }
        return 1;
    }
}
